package felix.rule.service.impl.install;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile("install")
@Slf4j
public class RuleEngineInstallService {
    @Autowired
    private ApplicationContext context;

    @Autowired
    private SqlDatabaseInstallService sqlDatabaseInstallService;

    private final String schemaSql = "schema-entity.sql";

    public void performInstall() {
        try {
            log.info("Installing SQL DataBase schema part: " + schemaSql);
            sqlDatabaseInstallService.loadSql(schemaSql);
        } catch (Exception e) {
            log.error("Unexpected error during installation!", e);
            throw new RuntimeException("Unexpected error during installation!", e);
        } finally {
            SpringApplication.exit(context);
        }
    }
}
